# -*- coding:utf-8 -*-
__author__ = 'wangjvv'
'''
date: 2020/11/11
description: 处理用例依赖
'''
from util.handleExcel import openExcel
from util.handleRequest import request
from util.handleLog import do_logger
from jsonpath import jsonpath

class HandleDependCase():
    def __init__(self):
        pass

    def runDependCase(self,sheet_name,caseId):
        '''
        通过Excel中依赖的caseId,获取该case的请求路径，请求参数，请求头，请求方法
        caseId:依赖的用例ID
        '''
        caseRow = openExcel.getRowNumByCaseId(sheet_name,caseId) #获取依赖的caseId所在的行号
        method = openExcel.method(sheet_name,caseRow) #获取依赖case的请求方法
        url = openExcel.url(sheet_name,caseRow) #获取依赖case的请求路径
        header = openExcel.header(sheet_name,caseRow)  #获取依赖case的请求头
        param = openExcel.request_param(sheet_name,caseRow) #获取依赖case的请求参数
        do_logger.info('执行依赖用例：{}'.format(caseId))
        if method == 'get':
            response = request.get(url=url, params=param, headers=header)  # 获取接口返回  get
        else:
            response = request.post(url=url, data=param, headers=header)  # 获取接口返回 post
        do_logger.info('获取依赖用例的返回数据：{}'.format(response))
        return response

    def getDependData(self,sheet_name,caseId):
        '''
        通过依赖的字段获取对应的返回结果
        '''
        caseRow = openExcel.getRowNumByCaseId(sheet_name, caseId)  # 获取依赖的caseId所在的行号
        dependKey = openExcel.keyRely(sheet_name,caseRow)  #获取依赖的键
        response = self.runDependCase(sheet_name,caseId)
        return jsonpath(response,'$..{}'.format(dependKey))[0]

dependCase = HandleDependCase()

if __name__ == '__main__':
    print(dependCase.getDependData('customerQuery','customerQuery001'))

